import axios from 'axios'
import { axiosResponseConfigType } from '../interface'

const request = axios.create({
    timeout: 3000
})

// 拦截请求
request.interceptors.request.use(config => {
    // 获取token
    const token = localStorage.getItem('token')

    if (token) {
        config.headers = {
            ...config.headers,
            Authorization: token
        }
    }
    return config
})

// 拦截响应
request.interceptors.response.use(config => {
    // status: 网络响应码
    const data: axiosResponseConfigType = {
        ...config.data
    }

    return data
}, (err) => {
    // err：返回网络响应码不为200的请求
    return Promise.reject(err)
})

export default request
